package algotithm.weekendrace.leetcode251.test1;

/**
 * @author 25068
 * @Date 2021/7/25 10:24
 * @Title 5823. 字符串转化后的各位数字之和
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
//        String s = "leetcode";
//        int k = 2;
        String s = "iiii";
        int k = 2;
        int ans = solution.getLucky(s,k);
        System.out.println(ans);
    }


    public int getLucky(String s, int k) {
        String str = reverse(s);
        char[] c = str.toCharArray();
        int ans = test(c);
        if (k == 1) {
            return ans;
        } else {
            k = k - 1;
            for (int i = 0; i < k; i++) {
                ans = cnt(ans);
            }
        }
        return ans;
    }

    public int cnt(int num) {
        int ans = 0;
        int x = (num) % 10;
        int y = (num / 10) % 10;
        int t = (num / 100) % 10;
        int z = (num / 1000) % 10;
        ans = x + y + t + z;
        return ans;
    }

    public int  test(char[] ch) {
        int ans = 0;
        for (int i = 0; i < ch.length; i++) {
            ans += (int)(ch[i] - '0');
        }
       return ans;
    }

    public String reverse(String s) {
        StringBuilder sb = new StringBuilder();
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            sb.append(c[i] - 'a' + 1);
        }
        return sb.toString();
    }

}
